//
//  UIViewController+TZExtension.m
//  TZOCExtension
//
//  Created by Tim.Z on 2017/1/3.
//  Copyright © 2017年 Tim.Z. All rights reserved.
//

#import "UIViewController+TZExtension.h"
#import "objc/runtime.h"
static const void *OrtherTextInputKey = &OrtherTextInputKey;
static const char *ktz_superVCKey = "ktz_superVCKey";

@implementation UIViewController (TZExtension)
@dynamic ortherTextInput;

- (UIViewController *)tz_superVC{
    return [self tz_getAssociatedValueForKey:&ktz_superVCKey];
}

- (void)setTz_superVC:(UIViewController *)tz_superVC{
    [self tz_setAssociateWeakValue:tz_superVC withKey:&ktz_superVCKey];
}

- (void)tz_addRightNavItemWithTitle:(nullable NSString *)title target:(nullable id)target action:(nullable SEL)action{
    if (title == nil) {
        title = [[NSString alloc] init];
    }
    CGSize titleSize = [title sizeWithFont:[UIFont systemFontOfSize:17]];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 7, titleSize.width, 22)];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)tz_addRightNavItemWithImage:(nullable UIImage *)image target:(nullable id)target action:(nullable SEL)action{
    if (!image) {
        return;
    }
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
    [btn setImage:image forState:UIControlStateNormal];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    if (target && action) {
        [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)tz_setupForDismissKeyboard {
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    UITapGestureRecognizer *singleTapGR =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    UIPanGestureRecognizer *singlePanGR =
    [[UIPanGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(tapAnywhereToDismissKeyboard:)];
    UISwipeGestureRecognizer *singleSwipeGR =
    [[UISwipeGestureRecognizer alloc] initWithTarget:self
                                              action:@selector(tapAnywhereToDismissKeyboard:)];
    [singleSwipeGR setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft];
    
    __weak UIViewController *weakSelf = self;
    
    NSOperationQueue *mainQuene =[NSOperationQueue mainQueue];
    [nc addObserverForName:UIKeyboardWillShowNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view addGestureRecognizer:singleTapGR];
                    [weakSelf.view addGestureRecognizer:singlePanGR];
                    [weakSelf.view addGestureRecognizer:singleSwipeGR];
                }];
    [nc addObserverForName:UIKeyboardWillHideNotification
                    object:nil
                     queue:mainQuene
                usingBlock:^(NSNotification *note){
                    [weakSelf.view removeGestureRecognizer:singleTapGR];
                    [weakSelf.view removeGestureRecognizer:singlePanGR];
                    [weakSelf.view removeGestureRecognizer:singleSwipeGR];
                }];
}

- (void)tapAnywhereToDismissKeyboard:(UIGestureRecognizer *)gestureRecognizer {
    [self tz_tapAnywhereToDismissKeyboard];
}

- (void)tz_tapAnywhereToDismissKeyboard{
    [self.view endEditing:YES];
    
    if (self.navigationItem) {
        if ([self.navigationItem.titleView isKindOfClass:[UISearchBar class]]) {
            [self.navigationItem.titleView resignFirstResponder];
        }
    }
    
    if (self.ortherTextInput && [self.ortherTextInput respondsToSelector:@selector(resignFirstResponder)]) {
        [self.ortherTextInput resignFirstResponder];
    }
}


- (void)setOrtherTextInput:(id)ortherTextInput
{
    objc_setAssociatedObject(self, OrtherTextInputKey, ortherTextInput, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (id)ortherTextInput
{
    return objc_getAssociatedObject(self, OrtherTextInputKey);
}


@end
